package net.mihome.app.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.etsy.android.grid.StaggeredGridView;
import com.loopj.android.http.RequestParams;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.adapter.SampleAdapter;
import net.mihome.app.bean.Advert;
import net.mihome.app.bean.AdvertList;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.common.UIHelper;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.utils.RequestParamsFactory;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MihomeDianpuView extends StaggeredGridView {
	private SampleAdapter adapter;
	private List<Advert> listData=new ArrayList<Advert>();
	Handler handler;
	private AppContext appContext;
	private int begin=0;
	private int num=10;
	private MyViewLinster myViewLinster;
	public MihomeDianpuView(Context context) {
		super(context);
		afterInit();
		// TODO Auto-generated constructor stub
	}
	public MihomeDianpuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		afterInit();
		// TODO Auto-generated constructor stub
	}
	public MihomeDianpuView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		afterInit();
		// TODO Auto-generated constructor stub
	}
	@SuppressLint("NewApi") 
	public void afterInit(){
		adapter = new SampleAdapter(getContext(),R.layout.active_listitem, listData);
		setAdapter(adapter);
		appContext=(AppContext) getContext().getApplicationContext();
		setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Advert active =listData.get(position);
				UIHelper.showShopRedirect(view.getContext(), active);
			}
		});
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message arg0) {
				adapter.notifyDataSetChanged();
				if(myViewLinster!=null){
					myViewLinster.onLoadComplete();
				}
				return false;
			}
		});
	}
	public void initData(){
		if(myViewLinster!=null){
			myViewLinster.onBeforeLoad();
		}
		begin=0;
		num=10;
		RequestParams requestParams= RequestParamsFactory.newInstance(AppContext.getInstance());
		requestParams.add("begin",begin+"");
		requestParams.add("num",num+"");
		appContext.ASYNC_HTTP_CLIENT.get(URLs.MIHOME_GET_ADVERT_LIST,requestParams,new MiAsyncHttpResponseHandler<Map>() {
			@Override
			public void onSuccess(int arg0, Header[] arg1, ResponsePojo<Map> responsePojo) {
				AdvertList list =AdvertList.parse(responsePojo);
				if(list!=null&&!list.getAdvertList().isEmpty()){
					listData.clear();
					listData.addAll(list.getAdvertList());
					adapter.notifyDataSetChanged();
					if(myViewLinster!=null){
						myViewLinster.onLoadComplete();
					}
				}
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
				if(myViewLinster!=null){
					myViewLinster.onLoadComplete();
				}
				Toast.makeText(appContext,arg3.getMessage(),Toast.LENGTH_SHORT).show();
			}
		});
	}
	public MyViewLinster getMyViewLinster() {
		return myViewLinster;
	}
	public void setMyViewLinster(MyViewLinster myViewLinster) {
		this.myViewLinster = myViewLinster;
	}
	
}
